package com.wgts.wgts.mvp.model.imp;

import com.wgts.wgts.mvp.model.IHomePageContentFragmentModle;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.NetService;
import com.wgts.wgts.netmodle.TransformUtils;
import com.wgts.wgts.netmodle.req.ActivityListRequest;
import com.wgts.wgts.netmodle.req.AppModleRequest;
import com.wgts.wgts.netmodle.resp.ActivityListEntity;
import com.wgts.wgts.netmodle.resp.AppModleEntity;
import com.wgts.wgts.netmodle.resp.VipMessageEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;
import com.wgts.wgts.tools.CountDownTime;
import com.wgts.wgts.tools.CountDownTimerListener;
import com.wgts.wgts.tools.VipMessageDate;

import java.util.List;
import java.util.logging.Handler;

import rx.Observable;
import rx.Subscriber;

public class HomePageContentFragmentModle implements IHomePageContentFragmentModle {

    private NetService netService;
    private VipMessageDate vipMessageDate;
    private CountDownTime countDownTime;
    private CountDownTimerListener countDownTimerListener;
    private int position = 0;

    private int time = 1;

    public HomePageContentFragmentModle(NetService netService, VipMessageDate vipMessageDate, CountDownTime countDownTime) {
        this.netService = netService;
        this.vipMessageDate = vipMessageDate;
        this.countDownTime = countDownTime;
    }

    @Override
    public void initDataView( AppModleRequest appModleRequest , HttpResultSubscriber<List<AppModleEntity>> subscriber) {
        netService.appModle(appModleRequest, subscriber);
    }


    @Override
    public void startCutDown(final MVPResultSubscriber<VipMessageEntity> mvpResultSubscriber) {
        countDownTimerListener = new CountDownTimerListener() {
            @Override
            public void countDownTimerListener() {

                time--;
                if(time == 0){
                    time = 10;
                    if (position >= vipMessageDate.getList().size()-1){
                        position = 0;
                    }else {
                        position++;
                    }
                    Observable.create(new Observable.OnSubscribe<VipMessageEntity>() {
                        @Override
                        public void call(Subscriber<? super VipMessageEntity> subscriber) {
                            subscriber.onNext(vipMessageDate.getList().get(position));
                        }
                    }).compose(TransformUtils.<VipMessageEntity>defaultSchedulers()).subscribe(mvpResultSubscriber);
                }
            }
        };
        countDownTime.addCountDownTimerListener(countDownTimerListener);
    }


    @Override
    public void removeCutDown() {
        countDownTime.removeCountDownTimerListener(countDownTimerListener);
    }

    @Override
    public void timeActionList(ActivityListRequest request, HttpResultSubscriber<List<ActivityListEntity>> subscriber) {
        netService.activityList(request, subscriber);
    }

    @Override
    public void vipMessage(HttpResultSubscriber<List<VipMessageEntity>> subscriber) {
        netService.vipMessage(subscriber);
    }


}
